$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'quoteParts/quoteparts/list',
        datatype: "json",
        colModel: [			
			{ label: '部件id', name: 'id', index: 'id', width: 50, key: true ,align:'center' },
			{ label: '分类名称', name: 'categoryName', index: 'category_name', width: 80 },
			{ label: '部件名称', name: 'partsName', index: 'parts_name', width: 80 }, 			
			{ label: '是否是主体', name: 'isMainPart', index: 'is_main_part', width: 80,
				 formatter: function (value) {
                     return value?"是":"否"
                 }
			},
			{ label: '排序', name: 'partsSort', index: 'parts_sort', width: 80 }, 			
			{ label: '是否屏蔽', name: 'delFlag', index: 'del_flag', width: 80,align:'center',
                formatter: function (value) {
                    return value?"是":"否"
                }
			},
			{ label: '是否在前端显示', name: 'display', index: 'display', width: 80,align:'center',
                formatter: function (value) {
                    return value?"显示":"不显示"
                }
			},
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 }, 			
			{ label: '创建人员', name: 'createUser', index: 'create_user', width: 80 }, 			
			{ label: '更新时间', name: 'updateDate', index: 'update_date', width: 80 }, 			
			{ label: '更新人员', name: 'updateUser', index: 'update_user', width: 80 }, 			
			{ label: '部件是否必选', name: 'mustChoice', index: 'must_choice', width: 80,align:'center',
                formatter: function (value) {
                    return value?"必选":"自由选择"
                }

			},
			{ label: '是否自来纸', name: 'selfPaper', index: 'self_paper', width: 80,align:'center',
			  formatter: function (value) {
                  return value?"是":"否"
              }
			},
        ],
		viewrecords: true,
       	height: window.innerHeight - 120,
        rowNum: 25,
		rowList : [25,50,100],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "categoryId",
            pIdKey: "parentCategoryId",
            rootPId: -1
        },
        key: {
            name: "categoryName",
            url:"nourl"
        }
    },
    height: 800,
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		quoteParts: {categoryName:null},
        /* 查询 */
        q:{
            categoryName: '',
            partsName: '',
        },
	},
	methods: {
        getGoodsCategory: function(){
            //加载商品分类树
            $.get(baseURL + "goodsCategory/goodscategory/select", function(r){
                ztree = $.fn.zTree.init($("#goodsCategoryTree"), setting, r.goodsCategoryEntityList);
                //如果是编辑,则选中
				;
                if(!isBlank(vm.quoteParts.categoryId)){
                    var node = ztree.getNodeByParam("categoryId", parseInt(vm.quoteParts.categoryId));
                    ztree.selectNode(node);
                    vm.quoteParts.categoryName = node.categoryName;
				}
            })
        },
        goodsCategoryTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择商品分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#goodsCategoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                	//数据同步
					;
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.quoteParts.categoryId = node[0].categoryId;
                    vm.quoteParts.categoryName = node[0].categoryName;
                    layer.close(index);
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.quoteParts = {categoryName:null};
			vm.getGoodsCategory();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)

		},
		saveOrUpdate: function (event) {
			var url = vm.quoteParts.id == null ? "quoteParts/quoteparts/save" : "quoteParts/quoteparts/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.quoteParts),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "quoteParts/quoteparts/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "quoteParts/quoteparts/info/"+id, function(r){
                vm.quoteParts = r.quoteParts;
                vm.getGoodsCategory();
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'categoryName': vm.q.categoryName.trim(),
                    'partsName': vm.q.partsName.trim(),
                },
                page:page
            }).trigger("reloadGrid");
		},
		reset: function (event) {
            vm.q.categoryName = '';
            vm.q.partsName = '';
            vm.reload();
        },
	}
});